package com.example.fw;

import java.util.Properties;
import org.netbeans.jemmy.ClassReference;
import org.netbeans.jemmy.operators.JFrameOperator;

public class ApplicationManager {
	
	private static ApplicationManager singleton;
	private Properties props;
	private FolderHelper folderHelper;
	private JFrameOperator mainFrame;
	private MenuHelper menuHelper;
	
	public static ApplicationManager getInstance() {
		if  (singleton == null)	{
			singleton = new ApplicationManager();
		}
		return singleton;
	}
	
	public void stop() {
		getApplication().requestClose();
	}

	public void serProperties(Properties props) {
		this.props = props;
	}
	
	public String getProperty(String key)	{
		return props.getProperty(key);
	}

	public String getProperty(String key, String defaulValue)	{
		return props.getProperty(key);
	}

	// lesson 6 - 28:40
	public JFrameOperator getApplication() {
		if (mainFrame == null)	{
			try {
				new ClassReference("addressbook.AddressBookFrame").startApplication();
				mainFrame = new JFrameOperator("jAddressBook");	// to find window of java applic with "jAddressBook" inside head [34:00]
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return mainFrame;				
	}

	public FolderHelper getFolderHelper() {
		if (folderHelper == null)	{
			folderHelper = new FolderHelper(this);	// this - manager
		}
		return folderHelper;
	}

	public MenuHelper getMenuHelper() {
		if (menuHelper == null)	{
			menuHelper = new MenuHelper(this);	// this - manager
		}
		return menuHelper;
	}
}